<template>
	<div>
		<picker v-if="show_type=='input'&&showflag?true:false" :value="index" :range="stock_value_arr" @change="bindPickerChange" :disabled="disabled">
			<uni-easyinput prefixIcon="home-filled" :value="stock_value_arr[index]" :placeholderStyle="placeholderStyle" :styles="styles" disabled></uni-easyinput>
		</picker>

		<picker v-if="show_type=='button'?true:false" :value="index" :range="stock_value_arr" @change="bindPickerChange" :disabled="disabled">
			  <image src="@/static/images/base/change_wh.png" style="width: 100rpx;height: 100rpx;"></image>
		</picker>
	</div>
</template>

<script>
	import {
	  uniDataPicker,uniEasyinput
	} from "@dcloudio/uni-ui"
	export default {
		props: {
			show_type:{
				type:String,
				default:'input' //input button
			},
		    stock_id: { // 当前选中 五金仓
		        type: Number,
		        default: Vue.prototype.globalData.default_stock_id
		    },
			disable:{
				type:Boolean,
				default:false
			},
			isallot:{
				type:Boolean,
				default:false
			},
			disabled:{
				type:Boolean,
				default:false
			},
		},
		data() {
		  return {
			  stock_value_arr:[],
			  stock_id_arr:[],
			  index: 0,//五金仓
			  placeholderStyle:'font-size:17px;',
			  styles:{
			  	borderColor: '#999'
			  },
			  showflag:true
			}
		},
		components: {
			uniDataPicker,uniEasyinput
		},
		mounted(){
			if(uni.getStorageSync('myconfig')==''){
				this.showflag = false
				return
			}else{
				this.showflag = true
			}
			
			this.$Db.selectTableData('warehouse').then(list=>{
				let stock_arr = []
				if(list.length>0){
					for(var i=0;i<list.length;i++){
						this.stock_value_arr.push(list[i].name)
						this.stock_id_arr.push(list[i].id)
						if(this.stock_id === list[i].id){
							this.index = i
						}
						stock_arr.push({id:list[i].id,name:list[i].name})
					}
					//在线版，把仓库放进缓存中
					uni.setStorageSync('stockhouse_arr',stock_arr)
				}
			}).catch(err=>{
				console.log(err)
			}); 
			
			//更新仓库id
			uni.$on('sel_stock_id',(val) => {
				if(val!=this.stock_id){
					this.index = uni.getStorageSync('stockhouse_arr').findIndex(ele => ele.id === val);
					this.$emit('update:stock_id',val)
				}
			})
		},
		methods: {
			 bindPickerChange: function(e) {
			    this.index = e.detail.value
				let stock_id = parseInt(this.stock_id_arr[e.detail.value])
				
				this.$emit('update:stock_id',stock_id)
				if(this.show_type=='button'){
					//切換仓库逻辑
					let user = uni.getStorageSync('user')
					user.stock_id = stock_id
					user.stock_name = this.stock_value_arr[this.index]
					
					uni.setStorageSync('user',user)
					//更新标题
					this.$Func.setBarTitle()
				}
			 },
		}
	}
</script>
<style >
</style>